import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: GodK
 * Date: 2024-03-31
 * Time: 21:45
 */
public class Main {
    Stack<Integer> stack;
    Stack<Integer> minstack;

    public void MinStack() {
        stack = new Stack<>();
        minstack = new Stack<>();
    }

    public void push(int val) {
        stack.push(val);
        if(minstack.empty()){
            minstack.push(val);
            return;
        }
        Integer peekVal = minstack.peek();
        if(peekVal >= val){
            minstack.push(val);
            return;
        }
    }

    public void pop() {
        if(stack.empty())return;
        int popVal = stack.pop();
        if(popVal == minstack.peek()){
            minstack.pop();
        }
    }

    public int top() {
        if(stack.empty())return -1;
        return stack.peek();
    }

    public int getMin() {
        if(minstack.empty())return -1;
        return minstack.peek();
    }

    public static boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if (ch == '(' || ch == '[' || ch == '{') {
                stack.push(ch);
            } else {
                if (stack.empty()) {
                    return false;
                } else {
                    char c = stack.peek();
                    if (c == '(' && ch == ')' || c == '[' && ch == ']' || c == '{' && ch == '}') {
                        stack.pop();
                    } else {
                        return false;
                    }
                }
            }
        }
        return stack.empty();
    }
    public static boolean IsPopOrder (int[] pushV, int[] popV) {
        Stack<Integer> stack = new Stack<>();
        int j = 0;
        for(int i = 0 ; i < pushV.length ; i++){
            stack.push(pushV[i]);
            //int j = 0;
            while(j < popV.length && !stack.empty()
                    && stack.peek() == popV[j]){
                stack.pop();
                    j++;
            }
        }
        return stack.empty();
    }
    public static void main(String[] args) {
        //System.out.println(isValid("()"));
        int[] a1 = new int[]{4,5,3,2,1};
        int[] a2 = new int[]{1,2,3,4,5};
        System.out.println(IsPopOrder(a2,a1));
    }
}
